<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn">CLick</button>
    <script>
      function debounce() {
        let timer = null // 状态
        console.log(timer, 'timer')
        // 1
        return () => {
          // 2
          if (timer) clearTimeout(timer)
          timer = setTimeout(() => {
            console.log('我是防抖函数')
          }, 2000)
        }
      }
      // btn.onclick = debounce()
      // debounce()()
      // 作用域链(内部可以访问外部，外部不能访问内部)
      // let a = 1
      // console.log(a)
      function auto() {
        let timer = null
        return function () {
          console.log(timer, 'this')
          // console.log(a)
          timer = setTimeout(() => {}, 1000)
        }
      }
      auto()()
    </script>
  </body>
</html>
